<template>
	<el-container>
		<el-header>
			<div class="left-panel">
				<el-button icon="el-icon-plus" type="primary" @click="open_dialog" />
			</div>
			<div class="right-panel">
				<div class="right-panel-search">
					<el-input v-model.trim="reqParams.key" clearable placeholder="请输入工单号查询" />
					<el-button icon="el-icon-search" type="primary" @click="search" />
				</div>
			</div>
		</el-header>
		<el-main class="nopadding">
			<scTable ref="table" :api-obj="apiObj" :params="reqParams" row-key="id" stripe hideContextMenu>
				<!-- 固定列-选择列 -->
				<el-table-column prop="wo" label="工单单号" align="center" width="140" />
				<el-table-column prop="productCode" label="产品代码" align="center" width="140" />
				<el-table-column prop="product.itemName" label="产品名称" align="center" width="140" />
				<el-table-column prop="totalQty" label="总数量" align="center" width="80" />
				<el-table-column prop="onlineQty" label="上线数量" align="center" width="80" />
				<el-table-column prop="completedQty" label="完工数量" align="center" width="80" />
				<el-table-column prop="qualifiedQty" label="合格数量" align="center" width="80" />
				<el-table-column prop="unqualifiedQty" label="不合格数量" align="center" width="100" />
				<el-table-column prop="scrapQty" label="报废数量" align="center" width="80" />
				<el-table-column prop="status" label="状态" align="center" width="140">
					<template #default="scope">
						<el-tag :type="statusType(scope.row.status)">{{ scope.row.statusDesc }}</el-tag>
					</template>
				</el-table-column>
				<el-table-column prop="batchNo" label="批次号" align="center" width="80" />
				<el-table-column prop="planStartTime" label="计划开始时间" align="center" width="140" />
				<el-table-column prop="planEndTime" label="计划结束时间" align="center" width="140" />
				<el-table-column prop="actualStartTime" label="实际开始时间" align="center" width="140" />
				<el-table-column prop="actualEndTime" label="实际结束时间" align="center" width="140" />
				<el-table-column prop="sourceDesc" label="工单来源" align="center" width="80" />
				<el-table-column prop="createTime" label="创建时间" align="center" width="140" />
				<el-table-column align="center" fixed="right" label="操作" width="200">
					<template #default="scope">
						<el-button text type="primary" size="small" @click="open_detail(scope.row)">详细</el-button>
						<el-button
							text
							type="primary"
							size="small"
							:disabled="scope.row.status !== 10"
							@click="open_dialog(scope.row)"
							>编辑</el-button
						>
						<el-popconfirm title="确定删除吗？" @confirm="table_del(scope.row, scope.$index)">
							<template #reference>
								<el-button text type="primary" :disabled="scope.row.status !== 10" size="small">
									删除
								</el-button>
							</template>
						</el-popconfirm>
					</template>
				</el-table-column>
			</scTable>
		</el-main>

		<modify ref="modify" @complete="complete" />
		<detail ref="detail" />
	</el-container>
</template>

<script>
import modify from "./modify";
import detail from "./detail";

export default {
	components: {
		modify,
		detail,
	},
	data() {
		return {
			apiObj: this.$API.prodwo.page,
			reqParams: {
				key: "",
			},
		};
	},
	methods: {
		complete() {
			this.$refs.table.refresh();
		},
		search() {
			this.$refs.table.upData(this.reqParams);
		},
		statusType(status) {
			switch (status) {
				case 10: return 'info';
				case 20: return 'info';
				case 30: return '';
				case 40: return 'success';
				case 50: return 'Warning';
				case 60: return 'danger';
				default:
				return 'info';
			}
		},
		async table_del(row) {
			const resp = await this.$API.prodwo.delete.delete([row.id]);
			if (resp.code == 200) {
				this.$refs.table.refresh();
				this.$message.success("删除成功");
			} else {
				this.$alert(resp.message, "提示", { type: "error" });
			}
		},
		open_detail(row) {
			this.$refs.detail.open(row);
		},
		open_dialog(row) {
			if (row.id) {
				this.$refs.modify.open(row);
			} else {
				this.$refs.modify.open();
			}
		},
	},
};
</script>
